<?php
$page_title = 'eTAT: Schedule Survey';
$style = 'survey';
$script = 'survey';
require_once '../controller.php';
if(isset($params->sid)) {
	$survey = $instructor->getSurvey($params->sid );
} else {
	redirect('survey/');
}
if(isset($_POST['deliver_now'])) {
	redirect("survey/deliver.php?sid=$survey_id");
}
if(isset($_POST['schedule'])) {
	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='daily'){
	$string = '';
 		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			$today_date = date('Y-m-d');

 			for($i=0;$i<$frequency;$i++){
 				$date[$i] = date("Y-m-d",strtotime($today_date)+($i)*24*3600);

 			}
 		}elseif(isset($_POST['frequency2'])&& $_POST['frequency2']!=''){
 			$until_date = $_POST['frequency2'];
  			$unix_until_date = strtotime($until_date);
 			$today_date =date('Y-m-d');
 			$unix_today_date = strtotime($today_date);
            $i=0;
 			while($unix_today_date <$unix_until_date){
 				$date[$i]= date("Y-m-d",strtotime($today_date)+($i)*24*3600);
 				$unix_today_date = strtotime($date[$i]);
 				$i++;
 			}

 		}else{
 			$error ='Enter either frequency or until date  ';
 		}
 		if(!$error){
 		$string =implode(',',$date);
 		$string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update Daily Schedule'.mysql_error());
 		}
 	}

 	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='weekly'){

 		if(isset($_POST['select_day'])){
 			$selected_day = $_POST['select_day'];
 			for($i=0;$i<7;$i++){
 				$future_day = date('l',strtotime("+$i day"));
 				if($future_day==$selected_day){
 					$num = $i;
 					break;
 				}
 			}

 			$first_date = date('Y-m-d',strtotime("+$num day"));
  		}

  		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			 $date[0]=$first_date;
 			for($i=1;$i<$frequency;$i++){
 				$date[$i] = date("Y-m-d",strtotime($first_date)+($i*7)*24*3600);
  			}
 		}elseif(isset($_POST['frequency2'])&& $_POST['frequency2']!=''){
 			$until_date = $_POST['frequency2'];
  			$unix_until_date = strtotime($until_date);
 			$unix_first_date = strtotime($first_date);
            $i=0;
            $flag=true;
 			while($flag){
 				$next_date= date("Y-m-d",strtotime($first_date)+($i*7)*24*3600);
 				$unix_first_date = strtotime($next_date);
 				if($unix_first_date<$unix_until_date){
 					$date[$i]=$next_date;
  				}else{
  					$flag=false;
  				}
 				$i++;
 			}

 		}else{
 			$error ='Enter either frequency or until date  ';
 		}
 		if(!$error){
 		$string =implode(',',$date);
 		$string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update weekly Schedule'.mysql_error());
 		}
 	}

 	if(isset($_POST['set_frequency'])&& $_POST['set_frequency']=='monthly'){

 		if(isset($_POST['select_date'])){
 			$selected_date = $_POST['select_date'];
 			if($selected_date<10){
 				$selected_date='0'.$selected_date;
 			}
 			$year = date('Y');
 			$month = date('m');
 			$every_month_date = "$year-$month-$selected_date";

 		}

 		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			for($i=0;$i<$frequency;$i++){
 			$date[$i]= date('Y-m-d',strtotime(($every_month_date )."+$i month" ));
 			}
 		}elseif(isset($_POST['frequency2'])){
 			$until_date = $_POST['frequency2'];
 			$unix_until_date = strtotime($until_date);
 			$unix_selected_date = strtotime($every_month_date);
 			$flag=true;
 			$i=0;
 			while($flag){
 				$next_date= date("Y-m-d",strtotime(($every_month_date)."+$i month"));
 				$unix_selected_date = strtotime($next_date);
 				if($unix_selected_date<$unix_until_date){
 					$date[$i]=$next_date;
 				}else{
 					$flag=false;
 				}
 				$i++;
 			}
 		}else{
 			$error = 'Please enter either frequency or date';
 		}
 		if(!$error){
 	  $string = implode(',',$date);
 	  $string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update monthly Schedule'.mysql_error());
 		}

 	}

 	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='fixed'){
 		$i=0;
 		if(isset($_POST['date_1'])){
 			$date[$i] = $_POST['date_1'];
 		  $i++;
 		if(isset($_POST['date_2'])){
 			$date[$i] = $_POST['date_2'];
 			$i++;
 		}
 		}else{
 			$error = 'Please enter date to schedule';
 		}
 		if(!$error){
 		$string = implode(',',$date);
 	    $string = addslashes($string);

 	    $sql = "UPDATE `survey` SET `schedule`='$string', `last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update fixed date Schedule'.mysql_error());
 		}
 	}

 	if(!$error){
 		header("Location:selectgroups.php?sid=$survey_id");
 	}

 }
require_once '../header.php';
print '<h1>'.anchor('survey/','Surveys',array('noselect'=>false)).' > '.$survey->survey_name.' Schedule</h1>';
$form = new Form('schedule_survey','post');
$form->radioButton('frequency','Daily','daily',array('type'=>'radio','onclick'=>'showFrequency(this)'));
$form->radioButton('frequency','Weekly','weekly',array('type'=>'radio','onclick'=>'showFrequency(this)'));
$form->radioButton('frequency','Monthly','monthly',array('type'=>'radio','onclick'=>'showFrequency(this)'));
$form->radioButton('frequency','Fixed Dates','fixed',array('type'=>'radio','onclick'=>'showFrequency(this)'));
$form->selectStart('select_date','On Date',array('class'=>'invisible'));
for($i=1; $i<32; $i++) {
	$form->selectOption("date-$i",$i);
}
$form->selectEnd();
$form->selectStart('select_day','On Every',array('class'=>'invisible'));
$form->selectOption("day-1",'Sunday');
$form->selectOption("day-2",'Monday');
$form->selectOption("day-3",'Tuesday');
$form->selectOption("day-4",'Wednesday');
$form->selectOption("day-5",'Thursday');
$form->selectOption("day-6",'Friday');
$form->selectOption("day-7",'Saturday');
$form->selectEnd();

print $form->build();
// 	echo "<table>";
// 	echo "<td>Until Date: <input type='text' name='frequency2' size = '10px'>(YYYY-MM-DD)</td></tr>";
// 	echo "<tr id='date1'><td>Date 1: <input type='text' name='date_1' size='10px'></td></tr>";
// 	echo "<tr id='date2'><td>Date 2: <input type='text' name='date_2' size='10px'></td></tr>";
// 	echo "<tr><td>&nbsp;</td></tr>";
// 	echo "<tr><td><input type='submit' name='schedule' value='schedule'></td></tr>";
// 	echo "</table>";
?>
<div class='clear'></div>
<?php require_once '../footer.php'; ?>